package com.leizhi.demo.monitor.service.impl;

import com.leizhi.demo.monitor.cache.TokenCache;
import com.leizhi.demo.monitor.pojo.UserDto;
import com.leizhi.demo.monitor.service.UserService;
import com.leizhi.demo.monitor.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TokenCache tokenCache;


    @Override
    public String login(UserDto dto) {
        if (dto == null || dto.getUsername() == null || dto.getPassword() == null) {
            throw new RuntimeException("用户名与密码不能为空");
        }

        if (!"admin".equals(dto.getUsername()) || !"123456".equals(dto.getPassword())) {
            throw new RuntimeException("用户名或密码错误");
        }

        String token = TokenUtil.token(dto.getUsername(), dto.getPassword());
        tokenCache.add(token, dto.getUsername());
        return token;
    }

}
